home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / gnustuff / tos / emacs / emacs18.57 / 1857sr~1.zoo / source / callproc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-12-02  |  13.8 KB  |  564 lines

  1. /* Synchronous subprocess invocation for GNU Emacs.
  2.    Copyright (C) 1985, 1986, 1987, 1988, 1990 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU Emacs.
  5.  
  6. GNU Emacs is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 1, or (at your option)
  9. any later version.
  10.  
  11. GNU Emacs is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GNU Emacs; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. /**
  21.  **  (sjk)++ The space to build a temporary file name.
  22.  **          Dev null installed flag, do not install /dev/null
  23.  **          until emacs is run in interactive mode (not in dumps).
  24.  **/
  25. #if defined(atarist)
  26. char   at_tmpname[32];
  27. static short null_dev_inst = 0;
  28. #endif
  29.  
  30. #include <signal.h>
  31.  
  32. #include "config.h"
  33.  
  34. #include <sys/types.h>
  35. #define PRIO_PROCESS 0
  36. #include <sys/file.h>
  37. #ifdef USG5
  38. #include <fcntl.h>
  39. #endif
  40.  
  41. #ifndef O_RDONLY
  42. #define O_RDONLY 0
  43. #endif
  44.  
  45. #ifndef O_WRONLY
  46. #define O_WRONLY 1
  47. #endif
  48.  
  49. #include "lisp.h"
  50. #include "commands.h"
  51. #include "buffer.h"
  52. #include "paths.h"
  53.  
  54. #define max(a, b) ((a) > (b) ? (a) : (b))
  55.  
  56. Lisp_Object Vexec_path, Vexec_directory;
  57.  
  58. Lisp_Object Vshell_file_name;
  59.  
  60. #ifndef MAINTAIN_ENVIRONMENT
  61. /* List of strings to append to front of environment of
  62.    all subprocesses when they are started.  */
  63.  
  64. Lisp_Object Vprocess_environment;
  65. #endif
  66.  
  67. #ifdef BSD4_1
  68. /* Set nonzero when a synchronous subprocess is made,
  69.    and set to zero again when it is observed to die.
  70.    We wait for this to be zero in order to wait for termination.  */
  71. int synch_process_pid;
  72. #endif /* BSD4_1 */
  73.  
  74. Lisp_Object
  75. call_process_cleanup (fdpid)
  76.      Lisp_Object fdpid;
  77. {
  78.   register Lisp_Object fd, pid;
  79.   fd = Fcar (fdpid);
  80.   pid = Fcdr (fdpid);
  81. /**
  82.  **  (sjk)++ We close the file in the function from where the external
  83.  **          process was creaed. 
  84.  **/
  85. #if defined(atarist)
  86.   if (XFASTINT(fd)>0)  close (XFASTINT (fd));
  87. #else
  88.   close (XFASTINT (fd));
  89.   kill (XFASTINT (pid), SIGKILL);
  90. #endif
  91.   return Qnil;
  92. }
  93.  
  94. #ifdef VMS
  95. extern noshare char **environ;
  96. #else
  97. extern char **environ;
  98. #endif
  99.  
  100. DEFUN ("call-process", Fcall_process, Scall_process, 1, MANY, 0,
  101.   "Call PROGRAM in separate process.\n\
  102. Program's input comes from file INFILE (nil means /dev/null).\n\
  103. Insert output in BUFFER before point; t means current buffer;\n\
  104.  nil for BUFFER means discard it; 0 means discard and don't wait.\n\
  105. Fourth arg DISPLAY non-nil means redisplay buffer as output is inserted.\n\
  106. Remaining arguments are strings passed as command arguments to PROGRAM.\n\
  107. This function waits for PROGRAM to terminate;\n\
  108. if you quit, the process is killed.")
  109.   (nargs, args)
  110.      int nargs;
  111.      register Lisp_Object *args;
  112. {
  113.   Lisp_Object display, buffer, path;
  114.   int fd[2];
  115.   int filefd;
  116.   register int pid;
  117.   char buf[1024];
  118.   int count = specpdl_ptr - specpdl;
  119.   register unsigned char **new_argv
  120.     = (unsigned char **) alloca ((max (2, nargs - 2)) * sizeof (char *));
  121.   struct buffer *old = current_buffer;
  122.  
  123. /**
  124.  **  (sjk)++ Install bammi's plug&play null data sink.
  125.  **/
  126. #if defined(atarist)
  127.   if (!null_dev_inst)
  128.     { install_null();
  129.       null_dev_inst = 1;
  130.     }
  131. #endif
  132.  
  133.   CHECK_STRING (args[0], 0);
  134.  
  135.   if (nargs <= 1 || NULL (args[1]))
  136. #ifdef VMS
  137.     args[1] = build_string ("NLA0:");
  138. #else
  139.     args[1] = build_string ("/dev/null");
  140. #endif /* not VMS */
  141.   else
  142.     args[1] = Fexpand_file_name (args[1], current_buffer->directory);
  143.  
  144.   CHECK_STRING (args[1], 1);
  145.  
  146.   {
  147.     register Lisp_Object tem;
  148.     buffer = tem = args[2];
  149.     if (nargs <= 2)
  150.       buffer = Qnil;
  151.     else if (!(EQ (tem, Qnil) || EQ (tem, Qt)
  152.            || XFASTINT (tem) == 0))
  153.       {
  154.     buffer = Fget_buffer (tem);
  155.     CHECK_BUFFER (buffer, 2);
  156.       }
  157.   }
  158.  
  159.   display = nargs >= 3 ? args[3] : Qnil;
  160.  
  161.   {
  162.     register int i;
  163.     for (i = 4; i < nargs; i++)
  164.       {
  165.     CHECK_STRING (args[i], i);
  166.     new_argv[i - 3] = XSTRING (args[i])->data;
  167.       }
  168.     /* Program name is first command arg */
  169.     new_argv[0] = XSTRING (args[0])->data;
  170.     new_argv[i - 3] = 0;
  171.   }
  172.  
  173. /**
  174.  **  (sjk)++ Open temp file in R/W mode on the ST...
  175.  **/
  176. #if defined(atarist)
  177.   filefd = open (XSTRING (args[1])->data, O_RDWR , 666);
  178. #else
  179.   filefd = open (XSTRING (args[1])->data, O_RDONLY, 0);
  180. #endif
  181.  
  182.   if (filefd < 0)
  183.     {
  184.       report_file_error ("Opening process input file", Fcons (args[1], Qnil));
  185.     }
  186.   /* Search for program; barf if not found.  */
  187. /**
  188.  **  (sjk)++ When searching for executables check some obvious extensions.
  189.  **/
  190. #if defined(atarist)
  191.   openp (Vexec_path, args[0], ":ttp:tos:prg", &path, 1);
  192. #else
  193.   openp (Vexec_path, args[0], "", &path, 1);
  194. #endif
  195.  
  196.   if (NULL (path))
  197.     {
  198.       close (filefd);
  199.       report_file_error ("Searching for program", Fcons (args[0], Qnil));
  200.     }
  201.   new_argv[0] = XSTRING (path)->data;
  202.  
  203.   if (XTYPE (buffer) == Lisp_Int)
  204. #ifdef VMS
  205.     fd[1] = open ("NLA0:", 0), fd[0] = -1;
  206. #else
  207.     fd[1] = open ("/dev/null", O_WRONLY), fd[0] = -1;
  208. #endif /* not VMS */
  209.   else
  210.     {
  211.       pipe (fd);
  212. #if 0
  213.       /* Replaced by close_process_descs */
  214.       set_exclusive_use (fd[0]);
  215. #endif
  216.     }
  217.  
  218.   {
  219.     /* child_setup must clobber environ in systems with true vfork.
  220.        Protect it from permanent change.  */
  221.     register char **save_environ = environ;
  222.     register int fd1 = fd[1];
  223.     char **env;
  224.  
  225. #ifdef MAINTAIN_ENVIRONMENT
  226.     env = (char **) alloca (size_of_current_environ ());
  227.     get_current_environ (env);
  228. #else
  229.     env = environ;
  230. #endif /* MAINTAIN_ENVIRONMENT */
  231.  
  232.     pid = vfork ();
  233. #ifdef BSD4_1
  234.     /* cause SIGCHLD interrupts to look for this pid. */
  235.     synch_process_pid = pid;
  236. #endif /* BSD4_1 */
  237.  
  238.     if (pid == 0)
  239.       {
  240. /**
  241.  **  (sjk)++ Avoid some excess file closes.
  242.  **/
  243. #if !defined(atarist)
  244.     if (fd[0] >= 0)
  245.       close (fd[0]);
  246. #ifdef USG
  247. #ifdef HAVE_PTYS
  248.     setpgrp ();
  249. #endif
  250. #endif
  251. #endif /* !defined(atarist) */
  252.  
  253.     child_setup (filefd, fd1, fd1, new_argv, env);
  254.       }
  255.  
  256.     environ = save_environ;
  257.  
  258.     close (filefd);
  259.     close (fd1);
  260.   }
  261.  
  262.   if (pid < 0)
  263.     {
  264. /**
  265.  **  (sjk)++ Make sure both end of pipe get closed if vfork() fails
  266.  **/
  267. #if defined(atarist)
  268.       close (fd[1]);
  269. #endif
  270.  
  271.       close (fd[0]);
  272.       report_file_error ("Doing vfork", Qnil);
  273.     }
  274.  
  275.   if (XTYPE (buffer) == Lisp_Int)
  276.     {
  277. #ifndef subprocesses
  278.       wait_without_blocking ();
  279. #endif subprocesses
  280.       return Qnil;
  281.     }
  282.  
  283.   record_unwind_protect (call_process_cleanup,
  284.              Fcons (make_number (fd[0]), make_number (pid)));
  285.  
  286.  
  287.   if (XTYPE (buffer) == Lisp_Buffer)
  288.     Fset_buffer (buffer);
  289.  
  290.   immediate_quit = 1;
  291.   QUIT;
  292.  
  293.   {
  294.     register int nread;
  295.  
  296.     while ((nread = read (fd[0], buf, sizeof buf)) > 0)
  297.       {
  298.     immediate_quit = 0;
  299.     if (!NULL (buffer))
  300.       insert (buf, nread);
  301.     if (!NULL (display) && FROM_KBD)
  302.       redisplay_preserve_echo_area ();
  303.     immediate_quit = 1;
  304.     QUIT;
  305.       }
  306.   }
  307.  
  308. /**
  309.  **  (sjk)++ here we close the write side of the pipe. 
  310.  **/
  311. #if defined(atarist)
  312.   close(fd[0]);
  313. #endif
  314.  
  315.   /* Wait for it to terminate, unless it already has.  */
  316.   wait_for_termination (pid);
  317.  
  318.   immediate_quit = 0;
  319.  
  320.   set_buffer_internal (old);
  321.  
  322.   unbind_to (count);
  323.  
  324.   return Qnil;
  325. }
  326.  
  327. DEFUN ("call-process-region", Fcall_process_region, Scall_process_region,
  328.   3, MANY, 0,
  329.   "Send text from START to END to a process running PROGRAM.\n\
  330. Delete the text if DELETE is non-nil.\n\
  331. Put output in BUFFER, before point.  nil => discard it, t => current buffer.\n\
  332. Sixth arg DISPLAY non-nil means redisplay buffer as output is inserted.\n\
  333. Remaining args are passed to PROGRAM at startup as command args.\n\
  334. This function normally waits for the process to terminate;\n\
  335. if you quit, the process is killed.")
  336.   (nargs, args)
  337.      int nargs;
  338.      register Lisp_Object *args;
  339. {
  340.   register Lisp_Object filename_string, start, end;
  341.  
  342. /**
  343.  **  (sjk)++  ask for a temp file name.
  344.  **/
  345. #if !defined(atarist)
  346.   char tempfile[20];
  347. #endif
  348. #if defined(atarist)
  349.   tmpnam(at_tmpname);
  350. #else
  351.   strcpy (tempfile, "/tmp/emacsXXXXXX");
  352.   mktemp (tempfile);
  353. #endif
  354. #if !defined(atarist)
  355.   filename_string = build_string (tempfile);
  356. #else
  357.   filename_string = build_string (at_tmpname);
  358. #endif
  359.  
  360.   start = args[0];
  361.   end = args[1];
  362.   Fwrite_region (start, end, filename_string, Qnil, Qlambda);
  363.  
  364.   if (!NULL (args[3]))
  365.     Fdelete_region (start, end);
  366.  
  367.   args[3] = filename_string;
  368.   Fcall_process (nargs - 2, args + 2);
  369.  
  370. /**
  371.  **  (sjk)++ on the ST unlink the correct name...
  372.  **/
  373. #if !defined(atarist)
  374.   unlink (tempfile);
  375. #else
  376.   unlink(at_tmpname);
  377. #endif
  378.  
  379.   return Qnil;
  380. }
  381.  
  382. /* This is the last thing run in a newly forked inferior
  383.    either synchronous or asynchronous.
  384.    Copy descriptors IN, OUT and ERR as descriptors 0, 1 and 2.
  385.    Initialize inferior's priority, pgrp, connected dir and environment.
  386.    then exec another program based on new_argv.
  387.  
  388.    This function may change environ for the superior process.
  389.    Therefore, the superior process must save and restore the value
  390.    of environ around the vfork and the call to this function.
  391.  
  392.    ENV is the environment */
  393.  
  394. child_setup (in, out, err, new_argv, env)
  395.      int in, out, err;
  396.      register char **new_argv;
  397.      char **env;
  398. {
  399.   register int pid = getpid();
  400.  
  401. /**
  402.  **  (sjk)++ No need to set proirity or close process descriptors...
  403.  **/
  404. #if !defined(atarist)
  405.   setpriority (PRIO_PROCESS, pid, 0);
  406.  
  407. #ifdef subprocesses
  408.   /* Close Emacs's descriptors that this process should not have.  */
  409.   close_process_descs ();
  410. #endif
  411. #endif /* atarist */
  412.  
  413.   /* Note that use of alloca is always safe here.  It's obvious for systems
  414.      that do not have true vfork or that have true (stack) alloca.
  415.      If using vfork and C_ALLOCA it is safe because that changes
  416.      the superior's static variables as if the superior had done alloca
  417.      and will be cleaned up in the usual way.  */
  418.  
  419.   if (XTYPE (current_buffer->directory) == Lisp_String)
  420.     {
  421.       register unsigned char *temp;
  422.       register int i;
  423.  
  424.       i = XSTRING (current_buffer->directory)->size;
  425.       temp = (unsigned char *) alloca (i + 2);
  426.       bcopy (XSTRING (current_buffer->directory)->data, temp, i);
  427.       if (temp[i - 1] != '/') temp[i++] = '/';
  428.       temp[i] = 0;
  429.       chdir (temp);
  430.     }
  431.  
  432. #ifndef MAINTAIN_ENVIRONMENT
  433.   /* Set `env' to a vector of the strings in Vprocess_environment.  */
  434.   {
  435.     register Lisp_Object tem;
  436.     register char **new_env;
  437.     register int new_length;
  438.  
  439.     new_length = 0;
  440.     for (tem = Vprocess_environment;
  441.      (XTYPE (tem) == Lisp_Cons
  442.       && XTYPE (XCONS (tem)->car) == Lisp_String);
  443.      tem = XCONS (tem)->cdr)
  444.       new_length++;
  445.  
  446.     /* new_length + 1 to include terminating 0 */
  447.     env = new_env = (char **) alloca ((new_length + 1) * sizeof (char *));
  448.  
  449.     /* Copy the env strings into new_env.  */
  450.     for (tem = Vprocess_environment;
  451.      (XTYPE (tem) == Lisp_Cons
  452.       && XTYPE (XCONS (tem)->car) == Lisp_String);
  453.      tem = XCONS (tem)->cdr)
  454.       *new_env++ = (char *) XSTRING (XCONS (tem)->car)->data;
  455.     *new_env = 0;
  456.   }
  457. #endif /* Not MAINTAIN_ENVIRONMENT */
  458.  
  459. /**
  460.  **  (sjk)++ the ST implementation of dup2() does a close so why do it twice.
  461.  **/
  462. #if !defined(atarist)
  463.   close (0);
  464.   close (1);
  465.   close (2);
  466. #endif
  467.  
  468.   dup2 (in, 0);
  469.   dup2 (out, 1);
  470. /**
  471.  **  (sjk)++ why close right after duping?
  472.  **/
  473. #if !defined(atarist)
  474.   dup2 (err, 2);
  475.   close (in);
  476.   close (out);
  477.   close (err);
  478. #endif
  479.  
  480. #ifdef USG
  481. #ifndef HAVE_PTYS
  482.   setpgrp ();            /* No arguments but equivalent in this case */
  483. #endif
  484. #else
  485.   setpgrp (pid, pid);
  486. #endif /* USG */
  487.   setpgrp_of_tty (pid);
  488.  
  489. #ifdef vipc
  490.   something missing here;
  491. #endif vipc
  492.  
  493.   /* execvp does not accept an environment arg so the only way
  494.      to pass this environment is to set environ.  Our caller
  495.      is responsible for restoring the ambient value of environ.  */
  496.   environ = env;
  497.   execvp (new_argv[0], new_argv);
  498.  
  499.   write (1, "Couldn't exec the program ", 26);
  500.   write (1, new_argv[0], strlen (new_argv[0]));
  501.   _exit (1);
  502. }
  503.  
  504. init_callproc ()
  505. {
  506.   register char * sh;
  507.   extern char **environ;
  508.   register char **envp;
  509.   Lisp_Object execdir;
  510.  
  511.   /* Turn PATH_EXEC into a path.  `==' is just a string which we know
  512.      will not be the name of an environment variable.  */
  513.   Vexec_path = decode_env_path ("==", PATH_EXEC);
  514.   Vexec_directory = Ffile_name_as_directory (Fcar (Vexec_path));
  515.   Vexec_path = nconc2 (decode_env_path ("PATH", ""), Vexec_path);
  516.  
  517.   execdir = Fdirectory_file_name (Vexec_directory);
  518.   if (access (XSTRING (execdir)->data, 0) < 0)
  519.     {
  520.       printf ("Warning: executable/documentation dir (%s) does not exist.\n",
  521.           XSTRING (Vexec_directory)->data);
  522.       sleep (2);
  523.     }
  524.  
  525.   sh = (char *) egetenv ("SHELL");
  526.   Vshell_file_name = build_string (sh ? sh : "/bin/sh");
  527.  
  528. #ifndef MAINTAIN_ENVIRONMENT
  529.   /* The equivalent of this operation was done
  530.      in init_environ in environ.c if MAINTAIN_ENVIRONMENT */
  531.   Vprocess_environment = Qnil;
  532. #ifndef CANNOT_DUMP
  533.   if (initialized)
  534. #endif
  535.     for (envp = environ; *envp; envp++)
  536.       Vprocess_environment = Fcons (build_string (*envp),
  537.                     Vprocess_environment);
  538. #endif /* MAINTAIN_ENVIRONMENT */
  539. }
  540.  
  541. syms_of_callproc ()
  542. {
  543.   DEFVAR_LISP ("shell-file-name", &Vshell_file_name,
  544.     "*File name to load inferior shells from.\n\
  545. Initialized from the SHELL environment variable.");
  546.  
  547.   DEFVAR_LISP ("exec-path", &Vexec_path,
  548.     "*List of directories to search programs to run in subprocesses.\n\
  549. Each element is a string (directory name) or nil (try default directory).");
  550.  
  551.   DEFVAR_LISP ("exec-directory", &Vexec_directory,
  552.     "Directory that holds programs that come with GNU Emacs,\n\
  553. intended for Emacs to invoke.");
  554.  
  555. #ifndef MAINTAIN_ENVIRONMENT
  556.   DEFVAR_LISP ("process-environment", &Vprocess_environment,
  557.     "List of strings to append to environment of subprocesses that are started.\n\
  558. Each string should have the format ENVVARNAME=VALUE.");
  559. #endif
  560.  
  561.   defsubr (&Scall_process);
  562.   defsubr (&Scall_process_region);
  563. }
  564.